const express=require('express');
//创建商品路由
const p=express.Router();
const pool=require('../pool');

//1.查询商品类别  http://127.0.0.1:8888/v1/products
p.get('/fid',(req,res,next)=>{
	let obj=req.query;
	console.log(obj);
	pool.query('select * from  ljk_laptop_family',[obj.fid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:201,msg:"请输入正确的商品编号"});
		}else{
			res.send({code:200,msg:"查询成功",data:result});
		}
	});
});
//2.按商品类别查询商品  http://127.0.0.1:8888/v1/laptops/fid
p.get('/:fid',(req,res,next)=>{
	let obj=req.params;
	console.log(obj);
	pool.query('select * from  ljk_laptop where fid=?',[obj.fid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:201,msg:"请输入正确的商品编号"});
		}else{
			res.send({code:200,msg:"查询成功",data:result});
		}
	});
});

//2.按商品编号查询商品  http://127.0.0.1:8888/v1/laptops/pro
p.get('/pro/:lid',(req,res,next)=>{
	let obj=req.params;
	console.log(obj);
	pool.query('select * from  ljk_laptop where lid=?',[obj.lid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:201,msg:"请输入正确的商品编号"});
		}else{
			res.send({code:200,msg:"查询成功",data:result});
		}
	});
});
//3.按关键字查询商品  http://127.0.0.1:8888/v1/products/
p.get('/cha/:title',(req,res,next)=>{
	let obj=req.params;
	console.log(obj);
	pool.query("SELECT * FROM ljk_laptop where title like '%"+obj.title+"%'",[obj.title],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:200,msg:"不存在该商品"});
		}else{
			res.send({code:200,msg:"查询成功",data:result});
		}
	});
});

//2.查找轮播图  http://127.0.0.1:8888/v1/laptops/pro
p.get('/pic/:lid',(req,res,next)=>{
	let obj=req.params;
	console.log(obj);
	pool.query('select * from  ljk_laptop_pic where lid=?',[obj.lid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:201,msg:"请输入正确的商品编号"});
		}else{
			res.send({code:200,msg:"查询成功",data:result});
		}
	});
});

//2.收藏  http://127.0.0.1:8888/v1/laptops/pro
p.put('/',(req,res,next)=>{
	let obj=req.body;
	console.log(obj);
	pool.query('update ljk_laptop set ? where lid=?',[obj,obj.lid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.length===0){
			res.send({code:201,msg:"请输入正确的商品编号"});
		}else{
			res.send({code:200,msg:"修改成功",data:result});
		}
	});
});
//导出路由
module.exports=p;

